package com.gzhryc.shared_device.oem.code.user.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.payment.dao.db.PayAccountGroup;
import com.gzhryc.shared_device.common.payment.services.PayAccountGroupService;
import com.gzhryc.shared_device.oem.code.user.dao.UserTransferMoneyWXDao;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserTransferMoneyWX;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class UserTransferMoneyWXService {

    static Logger log = Logger.getLogger(UserTransferMoneyWXService.class);

    public static UserTransferMoneyWXService self() {
        return MultiDBTools.getService(UserTransferMoneyWXService.class, true);
    }

    public static UserTransferMoneyWXService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, UserTransferMoneyWXService.class, true);
    }

    UserTransferMoneyWXDao dao;

    public UserTransferMoneyWXService(String jdbcKey) {
        this.dao = new UserTransferMoneyWXDao(jdbcKey);
    }

    public List<UserTransferMoneyWX> findByUserId(Long userId){
        if(userId != null){
            Conditions conditions = new Conditions(UserTransferMoneyWX.class);
            conditions.and().is("userId",userId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<UserTransferMoneyWX> findByUserIdAndAccountGroupId(Long userId,Long accountGroupId) throws LogicException {
        if(userId != null && accountGroupId != null){
            PayAccountGroup accountGroup = PayAccountGroupService.self().getById(accountGroupId);
            if(accountGroup == null){
                throw new LogicException("未找到账号组");
            }

            if(StringTools.isBlank(accountGroup.getWxTransferAppId())){
                throw new LogicException("账号组未配置提现AppId");
            }

            Conditions conditions = new Conditions(UserTransferMoneyWX.class);
            conditions.and().is("userId",userId);
            conditions.and().is("appId",accountGroup.getWxTransferAppId());

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public UserTransferMoneyWX getByKeys(String appId, String openId, Long userId){
        if(StringTools.isNotBlank(appId) && StringTools.isNotBlank(openId) && userId != null){
            Conditions conditions = new Conditions(UserTransferMoneyWX.class);
            conditions.and().is("appId",appId);
            conditions.and().is("openId",openId);
            conditions.and().is("userId",userId);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean save(UserTransferMoneyWX wxUser) throws LogicException {
        UserTransferMoneyWX temp = getByKeys(wxUser.getAppId(),wxUser.getOpenId(), wxUser.getUserId());
        if(temp != null){
            Conditions conditions = new Conditions(UserTransferMoneyWX.class);
            conditions.and().is("appId",wxUser.getAppId());
            conditions.and().is("openId",wxUser.getOpenId());
            conditions.and().is("userId",wxUser.getUserId());
            try {
                return dao.updateNotNull(wxUser,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }else {
            wxUser.setCreateDate(new Date());
            try {
                return dao.insert(wxUser) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean add(UserTransferMoneyWX wxUser) throws LogicException {
        wxUser.setCreateDate(new Date());
        try {
            return dao.insert(wxUser) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(UserTransferMoneyWX wxUser)throws LogicException{
        if(StringTools.isNotBlank(wxUser.getAppId()) && StringTools.isNotBlank(wxUser.getOpenId()) && wxUser.getUserId() != null) {
            Conditions conditions = new Conditions(UserTransferMoneyWX.class);
            conditions.and().is("appId",wxUser.getAppId());
            conditions.and().is("openId",wxUser.getOpenId());
            conditions.and().is("userId",wxUser.getUserId());
            try {
                return dao.updateNotNull(wxUser,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }

        return false;
    }
}
